package com.mdd.front.controller.zdjd;

import com.mdd.common.aop.NotLogin;
import com.mdd.common.aop.NotPower;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.zdjd.TZdjdDevice;
import com.mdd.common.entity.zdjd.TZdjdDeviceData;
import com.mdd.common.entity.zdjd.TZdjdDeviceStateRecord;
import com.mdd.common.entity.zdjd.param.DeviceBasicInfoParam;
import com.mdd.common.entity.zdjd.param.DeviceDataParam;
import com.mdd.common.entity.zdjd.param.DeviceRunStatusParam;
import com.mdd.front.service.zdjd.ITZdjdDeviceDataService;
import com.mdd.front.service.zdjd.ITZdjdDeviceService;
import com.mdd.front.service.zdjd.ITZdjdDeviceStateRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

//@ApiIgnore
@RestController
@Api(tags = "中盾金达---设备回传数据api")
@RequestMapping("/api/deviceFeedbackData")
public class DeviceFeedbackDataController {

    @Resource
    ITZdjdDeviceService deviceService;

    @Resource
    ITZdjdDeviceStateRecordService deviceStateRecordService;

    @Resource
    ITZdjdDeviceDataService deviceDataService;

    /**
     * 设备基础信息模块
     * @param param
     * @return
     */
    @NotPower
    @NotLogin
    @ApiOperation(value = "设备基础信息模块")
    @PostMapping(value = "/receiveDeviceBasicInfo", produces = "application/json")
    public String receiveDeviceBasicInfo(@RequestBody DeviceBasicInfoParam param){
        String id = param.getId();
        String model = param.getModel();
        String swVer = param.getSwVer();
        String hwVer = param.getHwVer();
        if(id==null || id.isEmpty() || model==null || model.isEmpty() || swVer==null || swVer.isEmpty() || hwVer==null || hwVer.isEmpty()){
            return JsonReturn.fail("参数不能为空！",40001).toJsonStr();
        }
        TZdjdDevice deviceData = deviceService.selectDeviceByUuidAndCode(id,model);
        if(deviceData!=null && deviceData.getId()!=null){
            return JsonReturn.fail("设备已存在",40002).toJsonStr();
        }
        TZdjdDevice device = DeviceBasicInfoParam.coverToTZdjdDevice(param);
        deviceService.insertDevice(device);
        return JsonReturn.success()
                .put("Id",device.getId())
                .put("model",device.getDeviceCode())
                .put("stat",device.getStatus())
                .put("lock", Optional.ofNullable(device.getLock()).orElse(0))
                .toJsonStr();
    }

    /**
     * 设备运行状态模块
     * @param param
     * @return
     */
    @NotPower
    @NotLogin
    @ApiOperation(value = "设备运行状态模块")
    @PostMapping(value = "/receiveDeviceRunStatus", produces = "application/json")
    public String receiveDeviceRunStatus(@RequestBody DeviceRunStatusParam param){
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        String id = param.getId();
        String model = param.getModel();
        Integer stat = param.getStat();
        String aCode = param.getACode();
        String ts = param.getTs();
        Double lon = param.getLon();
        Double lat = param.getLat();
        if(id==null || id.isEmpty() || model==null || model.isEmpty() || stat==null || aCode==null || aCode.isEmpty() || ts==null || ts.isEmpty() || lon==null || lat==null){
            return JsonReturn.fail("参数不能为空！",40001).toJsonStr();
        }
        TZdjdDevice device = deviceService.selectDeviceByUuidAndCode(id,model);
        if(device==null || device.getId()==null){
            return JsonReturn.fail("设备不存在",40002).toJsonStr();
        }
        TZdjdDeviceStateRecord deviceStateRecord = DeviceRunStatusParam.cover(param,device.getId());
        deviceStateRecordService.insertDeviceStateRecord(deviceStateRecord);
        if(!Objects.equals(device.getStatus(),stat)){
            device.setStatus(stat);
            device.setUpdateTime(now);
            deviceService.updateById(device);
        }
        return JsonReturn.success()
                .put("Id",device.getId())
                .put("model",device.getDeviceCode())
                .put("stat",device.getStatus())
                .toJsonStr();
    }

    /**
     * 设备参数模块
     * @param param
     * @return
     */
    @NotPower
    @NotLogin
    @ApiOperation(value = "设备参数模块")
    @PostMapping(value = "/receiveParameter", produces = "application/json")
    public String receiveDeviceParameter(@RequestBody DeviceDataParam param){
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        String id = param.getId();
        String model = param.getModel();
        if(id==null || id.isEmpty() || model==null || model.isEmpty()){
            return JsonReturn.fail("参数不能为空！",40001).toJsonStr();
        }
        TZdjdDevice device = deviceService.selectDeviceByUuidAndCode(id,model);
        if(device==null || device.getId()==null){
            return JsonReturn.fail("设备不存在",40002).toJsonStr();
        }
        TZdjdDeviceData deviceData = DeviceDataParam.coverToTZdjdDeviceData(param, device.getId());
        deviceDataService.insertDeviceData(deviceData);
        return JsonReturn.success()
                .put("Id",device.getId())
                .put("model",device.getDeviceCode())
                .put("stat",device.getStatus())
                .toJsonStr();
    }


}
